from option import Option
import gtk
import helpers
import signals
import mode
import gobject

class PreferencesWindow:
	def __init__(self):
		self.xml = helpers.loadGlade('prefs.glade')
		self.window = self.xml.get_widget('PreferencesWindow')
		self.tabs = self.xml.get_widget('NtbkTabs')

		self.defaultPreinspection = self.xml.get_widget('OptDefaultPreinspection')
		self.autoacceptTimes = self.xml.get_widget('OptAutoacceptTimes')
		self.stackmatMode = self.xml.get_widget('OptStackmatMode')

		self.modeDefault = self.xml.get_widget('OptModeDefault')
		self.modeSelector = self.xml.get_widget('DropModeSelector')
		self.modeBestTime = self.xml.get_widget('OptBestTime')
		self.modeBestAverage = self.xml.get_widget('OptBestAverage')

		self.modules = self.xml.get_widget('ListModules')
		self.moduleDetails = self.xml.get_widget('TxtModuleDetails')
				
		self.defaultPreinspection.set_value(int(Option().config.get('general.default_preinspection', 0)))
		self.autoacceptTimes.set_active(bool(int(Option().config.get('general.autoaccept_times', 0))))
		self.stackmatMode.set_active(bool(int(Option().config.get('general.stackmat', 0))))

		self.colorAvg = self.xml.get_widget('ColorBestAverage')
		self.colorBest = self.xml.get_widget('ColorBestTime')
		self.colorBestEmph = self.xml.get_widget('ColorBestTimeEmph')
		self.colorWorst = self.xml.get_widget('ColorWorstTime')
		self.colorWorstEmph = self.xml.get_widget('ColorWorstTimeEmph')

		self.recreating = False #workaround

		self.recreateModes()

		self.xml.signal_autoconnect(self)

		self.modeSelector.set_active(0)

		list = gtk.ListStore(str)
		self.modules.set_model(list)
		modCol = gtk.TreeViewColumn()

		self.modules.append_column(modCol)

		modCell = gtk.CellRendererText()
		modCol.pack_start(modCell, True)
		modCol.add_attribute(modCell, 'text',0)

		for mod in Option().modules:
			self.modules.get_model().append([mod])
		#self.window.set_transient_for(Option().windowMain.window)


		self.colorAvg.set_color(gtk.gdk.color_parse(Option().config.get('colors.best_average','#eef')))
		self.colorBest.set_color(gtk.gdk.color_parse(Option().config.get('colors.best','#efe')))
		self.colorBestEmph.set_color(gtk.gdk.color_parse(Option().config.get('colors.best_emphasis','#7f7')))
		self.colorWorst.set_color(gtk.gdk.color_parse(Option().config.get('colors.worst','#fee')))
		self.colorWorstEmph.set_color(gtk.gdk.color_parse(Option().config.get('colors.worst.emphasis','#f77')))

		signals.emit('prefs.init')
	
	def on_Color_color_set(self, widget):
		DESCRIPTOR = {
				self.colorAvg : 'colors.best_average',
				self.colorBest : 'colors.best',
				self.colorBestEmph : 'colors.best_emphasis',
				self.colorWorst : 'colors.worst',
				self.colorWorstEmph : 'colors.worst_emphasis',
				}

		Option().config.set(DESCRIPTOR[widget],widget.get_color().to_string())

	def recreateModes(self):
		self.recreating = True
		if self.modeDefault.get_model() != None:
			self.modeDefault.get_model().clear()
		if self.modeSelector.get_model() != None:
			self.modeSelector.get_model().clear()

		default=Option().config.get('general.default_mode',Option().modes[0])
		i = -1

		for mode in Option().modes:
			i+=1
			self.modeDefault.append_text(mode)
			self.modeSelector.append_text(mode)
			if mode == default:
				self.modeDefault.set_active(i)

		self.recreating = False

	def on_PreferencesWindow_destroy(self, widget):
		signals.emit('prefs.teardown')
		Option().config.save()
		Option().mode.load()
		Option().windowMain.refresh()
		Option().windowMain.recreateModeMenu(Option().mode.options().name)
		del Option().windowPreferences

	def on_ButClose_clicked(self, widget):
		self.window.destroy()

	def on_OptAutoacceptTimes_toggled(self, widget):
		Option().config.set('general.autoaccept_times', int(widget.get_active()))

	def on_OptStackmatMode_toggled(self, widget):
		Option().config.set('general.stackmat', int(widget.get_active()))

	def on_OptDefaultPreinspection_value_changed(self, widget):
		Option().config.set('general.default_preinspection', int(widget.get_value()))

	def on_OptModeDefault_changed(self, widget):
		if not self.recreating:
			Option().config.set('general.default_mode', Option().modes[widget.get_active()])

	def on_DropModeSelector_changed(self, widget):
		mode=Option().modes[widget.get_active()]

		self.modeBestTime.set_text(helpers.fmt(Option().config.get('modes.'+mode+'.best', -1.00)))
		self.modeBestAverage.set_text(helpers.fmt(Option().config.get('modes.'+mode+'.bestavg', -1.00)))

		signals.emit('prefs.mode.select')

	def on_ButModeAdd_clicked(self, widget):
		dial = gtk.Dialog('Add new mode', self.window, gtk.DIALOG_MODAL)

		dial.vbox.pack_start(gtk.Label('Type a name for the new mode:'))
		dial.vbox.pack_start(gtk.Entry())

		dial.add_button(gtk.STOCK_OK , gtk.RESPONSE_OK)
		dial.add_button(gtk.STOCK_CANCEL , gtk.RESPONSE_CANCEL)

		dial.connect('response', self.on_ButModeAdd_clicked_cont)

		dial.show_all()
		dial.run()

	def on_ButModeRemove_clicked(self, widget):
		actual=self.modeSelector.get_active()
		mode = Option().modes[self.modeSelector.get_active()]

		Option().modes.remove(mode)

		if Option().config.get('general.default_mode') == mode:
			Option().config.set('general.default_mode',Option().modes[0])

		"""if Option().mode.option().name == mode:
			Option().mode.teardown()
			Option().mode = mode.Mode(Option().modes[0])
"""
		signals.emit('mode.remove',mode)
		
		self.recreateModes()

		self.modeSelector.set_active(actual-1)

	def on_ButModeAdd_clicked_cont(self, widget, response):
		if response == gtk.RESPONSE_OK:
			mode = widget.vbox.get_children()[1].get_text()

			Option().modes.append(mode)
			self.recreateModes()
			self.modeSelector.set_active(len(self.modeSelector.get_model())-1)
			signals.emit('mode.add',mode)

		widget.destroy()

	def on_ButModeApply_clicked(self, widget):
		mode = Option().modes[self.modeSelector.get_active()]

		Option().config.set('modes.'+mode+'.best', helpers.unfmt(self.modeBestTime.get_text()))
		Option().config.set('modes.'+mode+'.bestavg', helpers.unfmt(self.modeBestAverage.get_text()))

	def on_ListModules_row_activated(self, widget, path, col):
		ret = ''
		iter = widget.get_model().get_iter(path)
		det = Option().modules[widget.get_model()[iter][0]]
		
		if 'name' in det:
			ret += 'Module name: '+det['name']+'\n'
		if 'version' in det:
			ret += 'Version: '+det['version']+'\n'
		if 'author' in det:
			ret += 'Author: '+det['author']+'\n'
		if 'url' in det:
			ret += 'Website: '+det['url']+'\n'
		if 'license' in det:
			ret += 'License: '+det['license']+'\n'
		if 'description' in det:
			ret += '\nDescription:\n '+det['description']+'\n'

		self.moduleDetails.get_buffer().set_text(ret)
	
